#pragma once
#include "public.hpp"

class Connection;
class Reactor;
using func_t = function<void(Connection *)>;

class Connection
{
public:
    func_t _recver;
    func_t _sender;
    func_t _execter;

    Reactor *_react;

private:
    int fd;
    Analyze addr; // 客户信息

    string inbuffer;
    string outbuffer;

    int events;

public:
    Connection(int sock)
        :fd(sock)
        ,_react(nullptr)
    {}

    void setevents(int eve) { events = eve; }
    void setReactor(Reactor *react) { _react = react; }

    void setfunc(func_t r, func_t s, func_t e)
    {
        _recver = r;
        _sender = s;
        _execter = e;
    }

    int getfd() { return fd; }
    string& GetInbuffer() { return inbuffer; }
    string& Getoutbuffer() { return outbuffer; }
    void Removebuffer(int size) { outbuffer.erase(0, size); }
    void Appendbuffer(string buffer) { outbuffer += buffer; }
    void Close() 
    { 
        if(fd >= 0)
            close(fd); 
    }

    ~Connection()
    {
    }
};